<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="大田编号" prop="fieldNumber">
        <el-input
          v-model="queryParams.fieldNumber"
          placeholder="请输入大田编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="中心点经度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入中心点经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中心点纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入中心点纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大田产量" prop="fieldYield">
        <el-input
          v-model="queryParams.fieldYield"
          placeholder="请输入大田产量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="株数" prop="plantCount">
        <el-input
          v-model="queryParams.plantCount"
          placeholder="请输入株数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="吐絮数" prop="openings">
        <el-input
          v-model="queryParams.openings"
          placeholder="请输入吐絮数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="铃数" prop="bolls">
        <el-input
          v-model="queryParams.bolls"
          placeholder="请输入铃数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="籽棉重量" prop="unginnedCottonWeight">
        <el-input
          v-model="queryParams.unginnedCottonWeight"
          placeholder="请输入籽棉重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="棉花品种" prop="variety">
        <el-input
          v-model="queryParams.variety"
          placeholder="请输入棉花品种"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后修改时间" prop="lastEditTime">
        <el-date-picker clearable
          v-model="queryParams.lastEditTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="大田备注" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入大田备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vbze:field:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['vbze:field:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vbze:field:remove']"
        >删除</el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['vbze:field:import']"
        >导入</el-button>
      </el-col>



      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vbze:field:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fieldList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="大田id" align="center" prop="id" />
      <el-table-column label="大田编号" align="center" prop="fieldNumber" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="中心点经度" align="center" prop="longitude" />
      <el-table-column label="中心点纬度" align="center" prop="latitude" />
      <el-table-column label="大田产量" align="center" prop="fieldYield" />
      <el-table-column label="株数" align="center" prop="plantCount" />
      <el-table-column label="吐絮数" align="center" prop="openings" />
      <el-table-column label="铃数" align="center" prop="bolls" />
      <el-table-column label="籽棉重量" align="center" prop="unginnedCottonWeight" />
      <el-table-column label="棉花品种" align="center" prop="variety" />
      <el-table-column label="最后修改时间" align="center" prop="lastEditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastEditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="大田备注" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vbze:field:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vbze:field:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改大田管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="大田编号" prop="fieldNumber">
          <el-input v-model="form.fieldNumber" placeholder="请输入大田编号" />
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="中心点经度" prop="longitude">
          <el-input v-model="<form action=" 
          " className=""></form>longitude" placeholder="请输入中心点经度" />
        </el-form-item>
        <el-form-item label="中心点纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入中心点纬度" />
        </el-form-item>
        <el-form-item label="大田产量" prop="fieldYield">
          <el-input v-model="form.fieldYield" placeholder="请输入大田产量" />
        </el-form-item>
        <el-form-item label="株数" prop="plantCount">
          <el-input v-model="form.plantCount" placeholder="请输入株数" />
        </el-form-item>
        <el-form-item label="吐絮数" prop="openings">
          <el-input v-model="form.openings" placeholder="请输入吐絮数" />
        </el-form-item>
        <el-form-item label="铃数" prop="bolls">
          <el-input v-model="form.bolls" placeholder="请输入铃数" />
        </el-form-item>
        <el-form-item label="籽棉重量" prop="unginnedCottonWeight">
          <el-input v-model="form.unginnedCottonWeight" placeholder="请输入籽棉重量" />
        </el-form-item>
        <el-form-item label="棉花品种" prop="variety">
          <el-input v-model="form.variety" placeholder="请输入棉花品种" />
        </el-form-item>
        <el-form-item label="最后修改人" prop="lastEditor">
          <el-input v-model="form.lastEditor" placeholder="请输入最后修改人" />
        </el-form-item>
        <el-form-item label="最后修改时间" prop="lastEditTime">
          <el-date-picker clearable
            v-model="form.lastEditTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="大田备注" prop="description">
          <el-input v-model="form.description" placeholder="请输入大田备注" />
        </el-form-item>
        <el-divider content-position="center">地块管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBlock">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBlock">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="blockList" :row-class-name="rowBlockIndex" @selection-change="handleBlockSelectionChange" ref="block">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="地块编号" prop="blockNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.blockNumber" placeholder="请输入地块编号" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.createTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="地块产量" prop="blockYield" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.blockYield" placeholder="请输入地块产量" />
            </template>
          </el-table-column>
          <el-table-column label="株数" prop="plantCount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.plantCount" placeholder="请输入株数" />
            </template>
          </el-table-column>
          <el-table-column label="吐絮数" prop="openings" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.openings" placeholder="请输入吐絮数" />
            </template>
          </el-table-column>
          <el-table-column label="铃数" prop="bolls" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.bolls" placeholder="请输入铃数" />
            </template>
          </el-table-column>
          <el-table-column label="籽棉重量" prop="unginnedCottonWeight" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.unginnedCottonWeight" placeholder="请输入籽棉重量" />
            </template>
          </el-table-column>
          <el-table-column label="棉花品种" prop="variety" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.variety" placeholder="请输入棉花品种" />
            </template>
          </el-table-column>
          <el-table-column label="最后修改时间" prop="lastEditTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.lastEditTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择最后修改时间" />
            </template>
          </el-table-column>
          <el-table-column label="地块备注" prop="description" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.description" placeholder="请输入地块备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的地块数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listField, getField, delField, addField, updateField } from "@/api/vbze/field";
import { getToken } from '@/utils/auth'

export default {
  name: "Field",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBlock: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大田管理表格数据
      fieldList: [],
      // 地块管理表格数据
      blockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,


      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的地块数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/vbze/field/importData"
      },

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fieldNumber: null,
        createTime: null,
        longitude: null,
        latitude: null,
        fieldYield: null,
        plantCount: null,
        openings: null,
        bolls: null,
        unginnedCottonWeight: null,
        variety: null,
        lastEditTime: null,
        description: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fieldNumber: [
          { required: true, message: "大田编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询大田管理列表 */
    getList() {
      this.loading = true;
      listField(this.queryParams).then(response => {
        this.fieldList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fieldNumber: null,
        creator: null,
        createTime: null,
        longitude: null,
        latitude: null,
        fieldYield: null,
        plantCount: null,
        openings: null,
        bolls: null,
        unginnedCottonWeight: null,
        variety: null,
        lastEditor: null,
        lastEditTime: null,
        description: null
      };
      this.blockList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加大田管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getField(id).then(response => {
        this.form = response.data;
        this.blockList = response.data.blockList;
        this.open = true;
        this.title = "修改大田管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.blockList = this.blockList;
          if (this.form.id != null) {
            updateField(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addField(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除大田管理编号为"' + ids + '"的数据项？').then(function() {
        return delField(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 地块管理序号 */
    rowBlockIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 地块管理添加按钮操作 */
    handleAddBlock() {
      let obj = {};
      obj.blockNumber = "";
      obj.createTime = "";
      obj.blockYield = "";
      obj.plantCount = "";
      obj.openings = "";
      obj.bolls = "";
      obj.unginnedCottonWeight = "";
      obj.variety = "";
      obj.lastEditTime = "";
      obj.description = "";
      this.blockList.push(obj);
    },
    /** 地块管理删除按钮操作 */
    handleDeleteBlock() {
      if (this.checkedBlock.length == 0) {
        this.$modal.msgError("请先选择要删除的地块管理数据");
      } else {
        const blockList = this.blockList;
        const checkedBlock = this.checkedBlock;
        this.blockList = blockList.filter(function(item) {
          return checkedBlock.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBlockSelectionChange(selection) {
      this.checkedBlock = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vbze/field/export', {
        ...this.queryParams
      }, `field_${new Date().getTime()}.xlsx`)
    },




    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "大田导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('vbze/field/importTemplate', {
      }, `field_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
